package com.example.lkfy.controller;

import com.example.lkfy.model.R;
import com.example.lkfy.entry.Tag;
import com.example.lkfy.model.RequestLimit;
import com.example.lkfy.param.TagParam;
import com.example.lkfy.server.TagService;
import com.gitee.fastmybatis.core.PageInfo;
import com.gitee.fastmybatis.core.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("tag")
public class TagController {
    @Autowired
    private TagService tagService;

    /**
     * 分页查询
     * @param param
     * @return
     */
    @RequestLimit(name = "token", limitTimes = 20, timeout = 60, timeUnit = TimeUnit.SECONDS)
    @GetMapping("/page")
    public R page(TagParam param) {
        Query query = param.toQuery();

        PageInfo<Tag> pageInfo = tagService.page(query);
        return R.ok().put("pageInfo", pageInfo);
    }

    /**
     * 保存
     * @param tag
     * @return
     */
    @GetMapping("/save")
    public R save(Tag tag) {
        tagService.saveIgnoreNull(tag);
        return new R().ok().put("id", tag.getId());
    }


    /**
     * 更新
     * @param tag
     * @return
     */
    @GetMapping("/update")
    public R update(Tag tag) {
        tagService.updateIgnoreNull(tag);
        return R.ok();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public R delete(Integer id) {
        tagService.deleteById(id);
        return R.ok();
    }
}
